package com.uchengwang.ui.cm_ui.umsg.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.umsg.bean.EnjoyChatBean;
import com.uchengwang.ui.cm_ui.umsg.ui.EnjoyRelationChainActivity;
import com.uchengwang.ui.cm_ui.umsg.ui.ShowWantChatAvatarActivity;
import com.uchengwang.ui.utils.DateUtil;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.emoticon.AndroidEmoji;
import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2018/3/21.
 */

public class EnterChatListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<EnjoyChatBean> list = new ArrayList<>();
    public EnterChatListAdapter(Context context){
        this.context = context;
    }

    public void setData(List<EnjoyChatBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<EnjoyChatBean> getData(){
        return list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_enter_list_item, parent, false);
        return new EnterListVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        EnterListVH holder = (EnterListVH) h;
        final EnjoyChatBean bean = list.get(position);
        holder.enter_name.setText(bean.getNickName());
        holder.enterTime.setText(DateUtil.getDateUtil().getTimeString(bean.getCreateTime()));

        SpannableStringBuilder spannable = new SpannableStringBuilder(bean.getContent());//匹配emoji表情
        AndroidEmoji.ensure(spannable);
        holder.enter_content.setText(spannable);
        ImageManager.getInstance().loadImage(context, holder.enter_avatar, bean.getUserAvatar(), ImageManager.ImageType.CIRCLE);
        if (bean.isFollow()){
            holder.enter_sign.setVisibility(View.VISIBLE);
            ImageManager.getInstance().loadImage(context, holder.enter_sign, R.drawable.icon_friend_sign, ImageManager.ImageType.NARMOL);
        }else {
            holder.enter_sign.setVisibility(View.GONE);
        }

        holder.enter_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ShowWantChatAvatarActivity.class);
                intent.putExtra(BaseConfig.USER_AVATAR, bean.getUserAvatar());
                context.startActivity(intent);
            }
        });

        holder.enter_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UCommonUtil.getUtil().toChatActiviy(context, bean.getUid(), Conversation.ConversationType.PRIVATE, bean.getNickName());
            }
        });

        holder.enter_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, EnjoyRelationChainActivity.class);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class EnterListVH extends RecyclerView.ViewHolder{
        private TextView enterTime, enter_name, enter_contact, enter_content;
        private ImageView enter_avatar, enter_sign;
        public EnterListVH(View itemView) {
            super(itemView);
            AutoUtils.auto(itemView);

            enterTime  = itemView.findViewById(R.id.enterTime);
            enter_name  = itemView.findViewById(R.id.enter_name);
            enter_contact  = itemView.findViewById(R.id.enter_contact);
            enter_content  = itemView.findViewById(R.id.enter_content);
            enter_avatar  = itemView.findViewById(R.id.enter_avatar);
            enter_sign  = itemView.findViewById(R.id.enter_sign);
        }
    }
}
